package com.example.controller.admin;

import com.example.common.Result;
import com.example.service.FileService;
import com.example.vo.FileUploadVO;
import com.example.utils.TencentCosUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
@RequestMapping("/admin/file")
public class FileController {

    @Resource
    private FileService fileService;

    @Resource
    private TencentCosUtil tencentCosUtil;

    @PostMapping("/upload")
    public Result<String> upload(@RequestParam("file") MultipartFile file) {
        try {
            // 上传文件到腾讯云COS
            String key = tencentCosUtil.uploadFile(file);
            // 获取文件完整访问URL
            String fileUrl = tencentCosUtil.getFileUrl(key);
            log.info("文件上传成功, URL: {}", fileUrl);
            return Result.success(fileUrl);
        } catch (Exception e) {
            log.error("文件上传失败: {}", e.getMessage());
            return Result.fail("文件上传失败: " + e.getMessage());
        }
    }

}
